use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub struct Topic {
    //
    pub peer_id: String,
    //
    pub topic: String,
    //
    pub subscribers: Vec<String>,
}

impl Topic {
    pub fn publish_topic(
        topic: &String,
        peer_id: &String,
        subscribers: Vec<String>,
    ) -> anyhow::Result<Topic> {
        Ok(Self {
            topic: topic.clone(),
            peer_id: peer_id.clone(),
            subscribers: subscribers,
        })
    }
    pub fn subscribe_topic(topic: &String, peer_id: &String) -> anyhow::Result<Topic> {
        Ok(Self {
            topic: topic.clone(),
            peer_id: peer_id.clone(),
            subscribers: vec![],
        })
    }
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub struct TopicMessage {
    //
    pub src: Option<String>,
    //
    pub topic: String,
    //
    pub message: String,
}

impl TopicMessage {
    pub fn new(topic: &String, message: String) -> anyhow::Result<TopicMessage> {
        Ok(Self {
            topic: topic.clone(),
            message: message,
            src: None,
        })
    }
}

// pub trait OnMessageCallback: Fn(&TopicMessage) + Send + Sync {}
